import axios from 'axios'
import { Message } from 'element-ui'
const ajaxRequestResolve = (config) => {
    config.validateStatus = (status) => {
        if (status === 404) {
            errorMessage('找不到相关的接口')
        } else if (status !== 200) {
            errorMessage('未知错误')
        }
        return true
    }
    return config
}

const ajaxResponseResolve = (response) => {
    // TODO 自定义code
    switch (response.code) {
        case 1:
            return respose.data
        case 2:
            Message.error(response.message || '接口异常')
    }
}
const instance = axios.create({
    baseURL: '',
    headers: {
        'X-Requested-With': 'XMLHttpRequest',
    },
})
instance.interceptors.request.use(
    (config) => {
        return ajaxRequestResolve(config)
    },
    (error) => {
        return Promise.reject(error)
    }
)

instance.interceptors.response.use(
    (response) => {
        return ajaxResponseResolve(response?.data)
    },
    (error) => {
        return Promise.reject(error)
    }
)

export default instance
